import requests
import json
import pandas as pd
Mai_key = "2042af1de5ec0cfa53a793235a023dcb"

# 地理编码
def geocode(address,city=None,batch=None,sig=None)->dict:
    """获取地理编码"""
    url = 'https://restapi.amap.com/v3/geocode/geo?parameters'
    params={
        'key': key_jn,
        'address':address,
        'city':city,
        'batch':batch,
        'sig':sig, 
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
广东省 = geocode(address='广东省')
print(广东省)
df_广东省地理编码 = pd.json_normalize(广东省['geocodes'])
display(df_广东省地理编码)
广东省地理编码 = 广东省['geocodes'][0]['location']
print("广东省地理编码:",广东省地理编码)

# 逆地理编码
def regeocode(location,poitype=None,radius=None,extensions="base",batch=False,roadlevel=None,sig=None,homeorcorp=None)->dict:
    """获取逆地理编码"""
    url = 'https://restapi.amap.com/v3/geocode/regeo?parameters'
    params={
        'key': key_jn,
        'location':location,
        'poitype':poitype,
        'radius':radius,
        'extensions':extensions,
        'batch':batch,
        'roadlevel':roadlevel,
        'homeorcorp':homeorcorp,
        'sig':sig,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data

中大南方逆地理编码 = regeocode(中大南方地理编码)
print(中大南方逆地理编码)
df_中大南方逆地理编码 = pd.json_normalize(中大南方逆地理编码).T
display(df_中大南方逆地理编码)

# A-2 Extra 全部逆地理编码分析
中大南方逆地理编码_all = regeocode(中大南方地理编码,extensions='all',radius=300)
print(中大南方逆地理编码_all)
df_中大南方逆地理编码_all = pd.json_normalize(中大南方逆地理编码_all)
display(df_中大南方逆地理编码_all)

# 路径规划
# 步行
def walking(origin,destination,sig=None)->dict:
    url = 'https://restapi.amap.com/v3/direction/walking?parameters'
    params={
        'key':key_jn,
        'origin':origin,
        'destination':destination,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
从化客运站 = geocode('广东省广州市从化区从化客运站')
从化客运站_location = 从化客运站['geocodes'][0]['location']
中大南方_location = 中大南方['geocodes'][0]['location']
print("(起点)中大南方_location:",中大南方_location,"(终点)从化客运站_location:",从化客运站_location)
中大南方_从化客运站 = walking(中大南方_location,从化客运站_location)
df_步行路径规划 = pd.json_normalize(中大南方_从化客运站["route"]["paths"][0]['steps'])
print(中大南方_从化客运站)
display(df_步行路径规划)
df_步行路径规划["instruction"]

# 公交车路线规划
def integrated(origin,destination,city,cityd=None,extensions='base',strategy=None,nightflag=0,date=None,time=None,sig=None)->dict:
    url = 'https://restapi.amap.com/v3/direction/transit/integrated?parameters'
    params={
        'key':key_jn,
        'origin':origin,
        'destination':destination,
        'city':city,
        'cityd':cityd,
        'extensions':extensions,
        'strategy':strategy,
        'nightflag':nightflag,
        'date':date,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data

# C-2 准备walking 参数
中大南方 = geocode('广东省广州市从化区中山大学南方学院')
中大南方_location = 中大南方['geocodes'][0]['location']
从化客运站 = geocode('广东省广州市从化客运站')
从化客运站_location = 从化客运站['geocodes'][0]['location']
print("(起点)中大南方_location:",中大南方_location,"(终点)从化客运站_location:",从化客运站_location)

# C-3 公交路径规划
中大南方_从化客运站 = integrated(中大南方_location,从化客运站_location,city='广州',extensions='all')
print(中大南方_从化客运站)
df_bus = pd.json_normalize(中大南方_从化客运站)
df_bus

 
# 行政区域查询
def district(keywords,subdistrict=None,page=None,offset=None,extensions='base',filter=None,)->dict:
    url = 'https://restapi.amap.com/v3/config/district?parameters'
    params={
        'key':key_jn,
        'keywords':keywords,
        'subdistrict':subdistrict,
        'page':page,
        'offset':offset,
        'extensions':extensions,
        'filter':filter,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
行政区域 = district(keywords='广州市',extensions='base',subdistrict=3)
行政区域
df_行政区域_1级 = pd.json_normalize(行政区域["districts"][0]['districts'])
df_行政区域_1级
df_行政区域_2级_广州市 = pd.json_normalize(行政区域["districts"][0]['districts'][3]['districts'])
df_行政区域_2级_广州市


# 搜索POI
# 关键字搜索
def place_text(keywords,types,city=None,citylimit=None,children=None,page=None,extensions='base',sig=None)->dict:
    url = 'https://restapi.amap.com/v3/place/text?parameters'
    params={
        'key':key_jn,
        'keywords':keywords,
        'types':types,
        'city':city,
        'citylimit':citylimit,
        'children':children,
        'page':page,
        'extensions':extensions,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
广州_中学 = place_text(keywords=None,types='中学',city="广州市",children=1,extensions='all',page=3)
print(广州_中学)
df_广州_中学 = pd.json_normalize(广州_中学["pois"])
df_广州_中学

# 周边搜索
# F-1 
def place_around(location,keywords=None,types=None,city=None,redius=None,sortrule=None,offset=None,page=None,extensions='base',sig=None)->dict:
    url = 'https://restapi.amap.com/v3/place/around?parameters'
    params={
        'key':key_jn,
        'keywords':keywords,
        'location':location,
        'types':types,
        'city':city,
        'redius':redius,
        'sortrule':sortrule,
        'offset':offset,
        'page':page,
        'extensions':extensions,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = response.json()
    return data
中大南方_周边POI = place_around(中大南方_location)
中大南方_周边POI
df_中大南方_周边POI = pd.json_normalize(中大南方_周边POI['pois'])
df_中大南方_周边POI

# 多边形搜索
def polygon(key,polygon="113.679287,23.632575|113.361200,23.124680",keywords="肯德基",types=None,offset="15",sig=None,extensions="all",page="1") ->dict:
    polygon_url = "https://restapi.amap.com/v3/place/polygon?parameters"
    params = {
        "key":key_jn,
        "polygon":polygon,
        "keywords":keywords,
        "types":types,
        "offset":offset,
        "sig":sig,
        "extensions":extensions,
        "page":page,
        "output":"json"
    }
    response = requests.get(polygon_url,params)
    data = response.json()
    return data
中大南方_天河城肯德基 = polygon("113.679287,23.632575|113.361200,23.124680")
中大南方_天河城肯德基


# IP定位
def ip_around(key,ip="192.168.43.139",sig=None)->dict:
    ip_url = "https://restapi.amap.com/v3/ip?parameters"
    params = {
        "key":key_jn,
        "ip":ip,
        "sig":sig,
        "output":"json"
    }
    response = requests.get(ip_url,params=params)
    data = response.json()
    return data
我的ip = ip_around('192.168.43.139')
我的ip

# 静态地图
from PIL import Image
from io import BytesIO
def staticmap(location,zoom,size=None,scale=1,markers=None,labels=None,paths=None,traffic=0,page=None,sig=None)->dict:
    url = 'https://restapi.amap.com/v3/staticmap?parameters'
    params={
        'key':key_jn,
        'location':location,
        'zoom':zoom,
        'size':size,
        'scale':scale,
        'markers':markers,
        'labels':labels,
        'paths':paths,
        'traffic':traffic,
        'sig':sig,
        'output':'json'
    }
    response = requests.get(url,params=params)
    data = Image.open(BytesIO(response.content))
    return data
staticmap(location=中大南方_location,zoom=16)

# 坐标转换
def convert(key,locations="13.679287,23.632575",coordsys="autonavi",sig=None)->dict:
    convert_url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
        "key":key_jn,
        "locations":locations,
        "coordsys":coordsys,
        "sig":sig,
        "output":"json"
    }
    response = requests.get(convert_url,params=params)
    data = response.json()
    return data
转换坐标 = convert("13.679287,23.632575")
转换坐标


#  天气查询
def weather(key,city="441900",extensions="all") ->dict:
    weather_url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        "key":key_jn,
        "city":city,
        "extensions":extensions,
    }
    response = requests.get(weather_url,params=params)
    data = response.json()
    return data
东莞天气预报 = weather("441900")
东莞天气预报

# 输入提示
def tips(key,keywords="麦当劳",city="440000",location="113.266530,23.132191",citylimit="true",datatype="all",sig=None) ->dict:
    tips_url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key":key_jn,
        "keywords":keywords,
        "city":city,
        "location":location,
        "citylimit":citylimit,
        "datatype":datatype,
        "sig":sig,
        "output":"json"
    }
    response = requests.get(tips_url,params=params)
    data = response.json()
    return data
麦当劳 = tips("440000")
麦当劳


# 交通态势
def rect(key,level="主要道路",extensions="all",rectangle="113.679287,23.632575;113.588823,23.547738") ->dict:
    rect_url = "https://restapi.amap.com/v3/traffic/status/rectangle?parameters"
    params = {
        "key":key_jn,
        "level":level,
        "extensions":extensions,
        "rectangle":rectangle,
        "output":"json"
    }
    response = requests.get(url=rect_url,params=params)
    data = response.json()
    return data
中大南方_从化客运站交通态势 = rect("113.679287,23.632575;113.588823,23.547738")
中大南方_从化客运站交通态势


# 批量请求接口
def batch(url="/v3/place/around?offset=10&page=1&key=2042af1de5ec0cfa53a793235a023dcb&location=116.315196,39.959971&output=json&radius=100000&types=080000"):
    parameters = {
        "key":key_jn,
    }
    body = {
        "ops":[
            {
                "url":'{}'.format(url)
            },
            {
                "url":"/v3/place/around?offset=10&page=1&key=2042af1de5ec0cfa53a793235a023dcbf&location=116.315196,39.959971&output=json&radius=100000&types=080000"
            }
        ]
    }
    response = requests.get("https://restapi.amap.com/v3/batch?",params = parameters,json=body)
    data = response.json()
    return data
batch()


# 地理围栏
def create(key,name="围栏",center="113.679287,23.632575",radius="3000",enable="true",valid_time ="2020-12-30",repeat="Mon,Tues,Wed,Thur,Fri,Sat,Sun",fixed_date ="2020-12-01;2020-12-03;2020-12-05",time="00:59,23:59",desc=None,alert_condition="enter;leave"):
    key_jn = "2042af1de5ec0cfa53a793235a023dcb"
    parameters = {
        "key":key_jn,
    }
    body = {
        "name": name,
        "center": center,
        "radius": radius,
        "enable": enable,
        "valid_time": valid_time,
        "repeat": repeat,
        "time": time,
        "desc": desc,
        "alert_condition": alert_condition
    }
    response = requests.post("https://restapi.amap.com/v4/geofence/meta",params = parameters,json = body)
    data = response.json()
    return data
create(key="key_jn",name="围栏",center="113.679287,23.632575")

# 轨迹纠偏
def Trajectory():
    parameters = {
        "key":key_jn,
    }
    body = [
        {
            "x": 116.449429,
            "y": 40.014844,
            "sp": 4,
            "ag": 110,
            "tm": 1478831753
        }, 
        {
            "x": 116.449639,
            "y": 40.014776,
            "sp": 3,
            "ag": 110,
            "tm": 23
        }, 
        {
            "x": 116.449859,
            "y": 40.014716,
            "sp": 3,
            "ag": 111,
            "tm": 33
        },
        {
            "x": 116.450074,
            "y": 40.014658,
            "sp": 3,
            "ag": 110,
            "tm": 31
        }, 
        {
            "x": 116.450273,
            "y": 40.014598,
            "sp": 3,
            "ag": 111,
            "tm": 20
        }]
    r = requests.get("https://restapi.amap.com/v4/grasproad/driving",params=parameters,json=body)
    data = r.json()
    return data
Trajectory()
